{% load i18n %}
{% include 'balances/temp_withdraw.html' %}
{% include 'balances/temp_deposit.html' %}

<script>
    function onclick_hide_small_assets(data) {
        $("#hide_small_assets").change(function() {
            var jsonobj = data['jsonobj']
            var checked = $("#hide_small_assets").is(":checked");
            if (checked == true) {
                for(i = 0; i < jsonobj.length; i++) {
                    var info = jsonobj[i]
                    var coin_type = info.coin_type
                    var key = "#" + coin_type + "_row"
                    var cnt = info.amount + info.freeze
                    if (cnt > 0.00001) {
                        $(key).show();
                    } else {
                        $(key).hide();
                    }
                }
            } else {
                for(i = 0; i < jsonobj.length; i++) {
                    var info = jsonobj[i]
                    var coin_type = info.coin_type
                    var key = "#" + coin_type + "_row"
                    $(key).show();
                }
            }
        })
    }

    function init_search(data) {
        $("#search").keyup(function() {
            var search_key = $("#search").val()
            console.log("search key "  + search_key)
            var jsonobj = data['jsonobj']
            for(i = 0; i < jsonobj.length; i++) {
                var flag = false
                var info = jsonobj[i]
                var name_upper = g_coin_info[info.coin_type].name_upper
                var cname = g_coin_info[info.coin_type].cname
                var coin_type = info.coin_type
                if (name_upper.includes(search_key)) {
                    flag = true
                } else if (cname.includes(search_key)) {
                    flag = true
                } else if (coin_type.includes(search_key)) {
                    flag = true
                }
                var key = "#" + coin_type + "_row"
                if (flag) {
                    $(key).show();
                } else {
                    $(key).hide();
                }
            }
            // 过滤显示
            //重新初始化 temp 并传入脚本,还是把tr设置为hidden
        });
    }

    //TODO 不知道为什么关闭无效，只能自己控制了
    var open_id = []

    function init_balances_collapse() {
        // 开启
        $(function () {
            $('.balance_detail_listen').on('show.bs.collapse', function () {
                var id =  $(this).attr('id');
                var curr_key = "#" + id + "_tr"
                var is_open = false
                // 判断一下是否自己，如果是自己，就是再次点开始
                for(var i=0; i<open_id.length; i++) {
                    if(open_id[i] == id) {
                        is_open = true
                    }
                }
                // 判断一下是否自己，如果是自己，就是再次点开始
                if (is_open) {
                    //console.log("4444 is open "  + is_open)
                    open_id.splice(id, 1);
                    var key = "#" + id + "_tr"
                    $(key).hide(100);
                    var key = "#" + id
                    $(key).collapse('hide')
                    return
                }
                $(curr_key).show();
                open_option(id)
                console.log("open  "  + id)
                // 把之前的都关闭
                for(i = 0; i < open_id.length; i++) {
                    var oid = open_id[i]
                    var key = "#" + oid + "_tr"
                    if (key == curr_key) {
                        continue
                    }
                    $(key).hide(100);
                    var key = "#" + oid
                    $(key).collapse('hide')
                }
                open_id = [id]
            });
        })

        // 关闭
        $(function () {
            $('.balance_detail_listen').on('hidden.bs.collapse', function () {
                var id =  $(this).attr('id');
                console.log("hide " + id)
                var key = "#" + id + "_tr"
                $(key).hide(100);
                // 把自己设置为关闭
                for(var i=0; i<open_id.length; i++) {
                    if(open_id[i] == id) {
                        open_id.splice(i, 1);
                    }
                }
            });
        })
    }

    function open_option(id) {
        //withdraw
        //console.log("id" + id)
        //var key = id.substr(0, "withdraw".length)
        if(id.substr(0, "withdraw".length) == "withdraw") {
            var coin_type =  id.substring("withdraw_".length)
            //console.log("whtidraw coin_type :"  + coin_type)
            withdraw_show(coin_type)
            return
        }
        if(id.substr(0, "deposit".length) == "deposit") {
            var coin_type =  id.substring("deposit_".length)
            //console.log("deposit coin_type :"  + coin_type)
            deposit_show(coin_type)
            return
        }
    }

    // 显示内容
    function withdraw_show(coin_type) {
        $.post("{% url 'withdraw'%}", {coin_type: coin_type}, function(data) {
            //console.log("withdraw return data")
            //console.log(data)
            var data = {
                info : data,
                g_coin_info: g_coin_info,
            }
            var html = template('temp_withdraw', data);
            //console.log("html " + html)
            var temp_key = '#withdraw_body_' +  coin_type
            //console.log("temp_key " + temp_key)
            $(temp_key).html(html)
            $('.click_amount').on("click",function(){
                var amount = $(this).data("amount");
                $('#amount').val(amount)
            })
        })
    }
    // 显示内容
    function deposit_show(coin_type) {
        $.post("{% url 'deposit'%}", {coin_type: coin_type}, function(data) {
            //console.log("deposit return data")
            //console.log(data)
            var data = {
                info : data,
            }
            var html = template('temp_deposit', data);
            //console.log("html " + html)
            var temp_key = '#deposit_body_' +  coin_type
            //console.log("temp_key " + temp_key)
            $(temp_key).html(html)
        })
    }
</script>

<script id="temp_balances_detail" type="text/html">
    <div>
        <div class="balance_search f-fl">
            <div class="search_input">
                <input type="text" id="search">
            </div>

            <div class="search_input_i">
                <i class="fa fa-search"></i>
            </div>
        </div>
        <div class="hide_small_assets f-fl">
            <input id="hide_small_assets" type="checkbox" > <label for="hidebalances" class="ng-binding">{% trans '隐藏小额资产' %}</label>
        </div>

        <div class="day-limit gray f-fr">
            <span>{% trans '账户总资产(估值)' %}</span>
            <span class="black">≈ <span id="total_amount_cny"></span>CNY</span>
            <!--
            <span>{% trans '日提现额度' %}</span>
            <span class="black">2 BTC</span>
            <span>{% trans '今日已用' %}</span>
            <span class="black">0 BTC </span>
            <a href="#" class="a-theme mg-l16">{% trans '提升提现额度' %}</a>
            todo-->
        </div>
    </div>

    <div class="context-body f-pz">
        <div class="panel-group" id="accordion">
            <table class="table table-hover">
                <thead>
                <tr class="bg-white">
                    <th>{% trans '币种' %}</th>
                    <th>{% trans '全称' %}</th>
                    <th>{% trans '总额' %}</th>
                    <th>{% trans '可用' %}</th>
                    <th>{% trans '委托or提现冻结' %}</th>
                    <th>{% trans 'CNY估值' %}</th>
                    <th colspan=3 class="opt">{% trans '操作' %}</th>
                </tr>
                </thead>
                <tbody>
                <% for(i = 0; i < jsonobj.length; i ++ ){ %>
                <% var info = jsonobj[i] %>
                <% var coin_type = info.coin_type %>
                <% if (coin_type == 'ttoken') { %>
                <% //continue %>
                <% } %>
                <% if (!g_coin_info[info.coin_type]) { %>
                <% $imports.log(info.coin_type) %>
                <% } %>
                <% var name_upper = g_coin_info[info.coin_type].name_upper %>
                <% var cname = g_coin_info[info.coin_type].cname %>
                <% var icon = $imports.get_coin_icon_url(info.coin_type) %>
                <% var tofixed = 8 %>
                <% var amount = $imports.floor_number(info.amount * 1, tofixed) %>
                <% var freeze = $imports.floor_number(info.freeze * 1, tofixed) %>
                <% var cnt = freeze + amount %>
                <% var ca = $imports.Calculate %>
                <% var amount = ca.DownTruncation(amount, 8) %>
                <% var freeze = ca.DownTruncation(freeze, 8) %>
                <% var cnt = ca.DownTruncation(cnt, 8) %>
                <% var deposit_open = info.deposit_open %>
                <% var withdraw_open = info.withdraw_open %>
                <% var amount_cny_id = info.coin_type + "_cnyid" %>
                <tr class="balance_btn" id="<%=info.coin_type%>_row">
                    <td id="balance-detail-first-td"><img src="<%=icon%>" width="14px" height="14px"/> <%=name_upper%></td>
                    <td><%=cname%></td>
                    <td><%=cnt%></td>
                    <td><%=amount%></td>
                    <td><%=freeze%></td>
                    <td><span id="<%=amount_cny_id%>">0.00</span></td>
                    <% if (coin_type == 'bitcny'){%>
                        <td><a href="/c2c/" class="btn btn-block red">{% trans 'C2C充值' %}</a></td>
                        <td><a href="/c2c/" class="btn btn-block green">{% trans 'C2C提现' %}</a></td>
                        <td><a href="/c2c/" class="btn btn-block">{% trans '交易' %}</a></td>
                    <% } else { %>
                        <% if (deposit_open == 0) { %>
                            <td><a data-toggle="collapse" data-parent="#accordion" href="#deposit_<%=coin_type%>" class="btn btn-block" disabled>{% trans '充值' %}</a></td>
                        <% } else { %>
                            <td><a data-toggle="collapse" data-parent="#accordion" href="#deposit_<%=coin_type%>" class="btn btn-block">{% trans '充值' %}</a></td>
                        <% } %>
                        <% if (withdraw_open == 0) { %>
                            <td><a data-toggle="collapse" data-parent="#accordion" href="#withdraw_<%=coin_type%>" class="btn btn-block" disabled>{% trans '提现' %}</a></td>
                        <% } else { %>
                            <td><a data-toggle="collapse" data-parent="#accordion" href="#withdraw_<%=coin_type%>" class="btn btn-block">{% trans '提现' %}</a></td>
                        <% } %>
                        <% var market_key = coin_type + '2bitcny'%>
                        <% if (g_markets[market_key]) { %>
                            <td><a href="/btc/trade?type=<%=coin_type%>2bitcny" class="btn btn-block">{% trans '交易' %}</a></td>
                        <% } else { %>
                            <td><a href="/btc/trade?type=<%=coin_type%>2bitcny" class="btn btn-block" disabled>{% trans '交易' %}</a></td>
                        <% } %>
                    <% } %>
                </tr>
                <tr id="withdraw_<%=coin_type%>_tr" style="display:none">
                    <td colspan=9>
                        <div id="withdraw_<%=coin_type%>" class="panel-collapse collapse balance_detail_listen">
                            <div class="panel-body" >
                                <div id="withdraw_body_<%=coin_type%>" ></div>
                            </div>
                        </div>
                    </td>
                </tr>
                <tr id="deposit_<%=coin_type%>_tr" style="display:none">
                    <td colspan=9>
                        <div id="deposit_<%=coin_type%>" class="panel-collapse collapse balance_detail_listen">
                            <div class="panel-body">
                                <div id="deposit_body_<%=coin_type%>"></div>
                            </div>
                        </div>
                    </td>
                </tr>
                <%}%>
                </tbody>
            </table>
        </div>
    </div>
</script>
